<template>
  <van-popup
      :show="is_show"
      round
      closeable
      custom-style="width:80%"
      @close="closePopup"
      class="popup-orderList"
    >
    <view class="hd">订单：{{info.billId}}</view>
    <van-cell-group class="bd">
      <van-cell v-for="(item,index) in list" :key="index" :title="item.projectName + '('+ item.price+'元)'" :label="item.desc" >
        <van-button round type="gray" size="mini" v-if="item.status == 4">已取消</van-button>
        <van-button round type="info" size="mini" v-if="item.status < 3" @click="cancelSignle(item)">取消</van-button>
      </van-cell>
    </van-cell-group>
    <view class="ft align-center">
      <van-button round size="small" type="info" @click="cancelAll">全部取消</van-button>
    </view>
  </van-popup>
</template>
<script>
export default{
  data(){
    return{
      is_show: false,
      info: null,
      list: [],
    }
  },
  methods:{
    init(row){
      this.info = row;
      this.getList();
      this.is_show = true;
    },
    getList(){
      this.$http('get', '/bill/getSuborderList', {billId: this.info.billId}, (data,msg) => {
        this.list = data;
        if(data.length > 0){
          this.list = data.map(item => {
            item.desc = this.getProjectDetail(item);
            return item;
          })
        }
        
      })
    },
    getProjectDetail(obj){
      let start_time = obj.time < 10 ? '0'+obj.time : obj.time;
      let end_time = obj.time+1;
      end_time = end_time < 10 ? '0'+ end_time : end_time;
      start_time += ':00';
      end_time += ':00';
      return obj.siteNo + '号场，' + start_time + '~' + end_time;
    },
    closePopup(){
      this.is_show = false;
    },
    cancelSignle(item){
      let arr = this.list.filter(item => item.status < 4);
      if(arr.length == 1){ // 如果只剩一个服务时，调取消订单的接口
        this.cancelAll();
        return;
      };
      let params = {
        id: item.id,
        status: 4,
      };
      uni.showModal({
				title: '提示',
				content: '确认要取消该服务吗？',
				success: res => {
					if(res.confirm){
            this.$http('put', '/suborder/cancel', params, (data,msg) =>{
              uni.showToast({
									title: '订单取消成功！',
									icon: 'none',
									mask: true,
									complete: () => {
										setTimeout(() => {
											this.getList();
										},500)
									}
								})
            })
					}
				}
			})
    },
    cancelAll(){
      let params = {
        billId: this.info.billId,
        status: 4,
      };
      uni.showModal({
				title: '提示',
				content: '确认要取消当前订单吗？',
				success: res => {
					if(res.confirm){
            this.$http('put', '/bill/handle', params, (data,msg) =>{
              uni.showToast({
									title: '订单取消成功！',
									icon: 'none',
									mask: true,
									complete: () => {
										setTimeout(() => {
											this.is_show = false;
                      this.$emit('confirm');
										},500)
									}
								})
            })
					}
				}
			})
    }
  }
}
</script>